/*
 * $Id: ArticleTag.java 46 2012-02-17 12:50:01Z rook.alex@gmail.com $
 */
package ua.org.energy.vkek.site.model;

import com.google.appengine.api.datastore.Key;
import java.io.Serializable;
import java.util.List;
import java.util.Stack;
import javax.persistence.*;
import ua.org.energy.vkek.site.server.DataIOException;

/**
 * Tag is a some Label for Aricle for filtering purposes
 */
@NamedQueries({
    @NamedQuery(name = "ArticleTag.findAll",
    query = "SELECT obj from ArticleTag obj"),
    @NamedQuery(name = "ArticleTag.findByName",
    query = "SELECT obj from ArticleTag obj WHERE obj.tagName=:tagName"),
    @NamedQuery(name = "ArticleTag.findCount",
    query = "SELECT count(obj) from ArticleTag obj"),
    @NamedQuery(name = "ArticleTag.findArticlesByTag",
    query = "SELECT obj from Article obj WHERE obj.tag=:tag")
})
@Entity
public class ArticleTag extends AbstractEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key ident;
    //@Column(unique = true) //UnsupportedOperationException: No support for uniqueness constraints :-(
    private String tagName;
    //A some 'Weight' of Tag for using in tag-cloud
    private int tagWeight;
    @ManyToOne(optional = false)
    private ArticleCategory category;
    @OneToMany(mappedBy = "tag")
    private List<Article> articles;

    /**
     * @return the tagName
     */
    public String getTagName() {
        return tagName;
    }

    /**
     * @param tagName the tagName to set
     */
    public void setTagName(String tagName) throws DataIOException {
        this.tagName = setFieldWise(this.tagName, tagName, true, new String[]{"", "undefined"});
    }

    /**
     * @return the tagWeight
     */
    public int getTagWeight() {
        return tagWeight;
    }

    /**
     * @param tagWeight the Weight to set
     */
    public void setTagWeight(int tagWeight) {
        this.tagWeight = tagWeight;
    }

    public int incWeight() {
        tagWeight++;
        return tagWeight;

    }

    public int decWeght() {
        if (tagWeight > 0) {
            tagWeight--;
        }
        return tagWeight;
    }

    /**
     * @return the category
     */
    public ArticleCategory getCategory() {
        return category;
    }

    /**
     * @param ArticleCategory the category to set
     */
    public void setCategory(ArticleCategory category) {
        this.category = category;
    }

    /**
     * @return the ident
     */
    public Key getIdent() {
        return ident;
    }

    /**
     * @return the articles
     */
    public List<Article> getArticles() {
        return articles;
    }

    /**
     * @param articles the articles to set
     */
    public void setArticles(List<Article> articles) {
        this.articles = articles;
    }

    /**
     * @param ident the ident to set
     */
    public void setIdent(Key ident) {
        this.ident = ident;
    }

    @Override
    public ArticleTag webSafeClone() {
        ArticleTag buf = new ArticleTag();
        try {
            buf.setIdent(ident);
            buf.setTagName(tagName);
            buf.setTagWeight(tagWeight);
        } finally {
            return buf;
        }
    }

    @Override
    public Stack<EntityPathElement> getEntityPath() {
        Stack<EntityPathElement> result;
        if (this.getCategory() != null) {
            result = getCategory().getEntityPath();
        } else {
            result = new Stack<EntityPathElement>();
        }
        result.push(getPathElement());
        return result;
    }

    @Override
    public EntityPathElement getPathElement() {
        return new EntityPathElementImpl(tagName, ident);
    }

    @Override
    public String toString() {
        return getClass().getName() + ",Key:"
                + getIdent().toString() + ",TagName:" + getTagName();
    }
}
